﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NJL.Web.Payment.chinabank
{
    public partial class Receive : BasePageVisitor
    {    
        protected string v_oid;		// 订单号
        protected string v_pstatus;	// 支付状态码
        //20（支付成功，对使用实时银行卡进行扣款的订单）；
        //30（支付失败，对使用实时银行卡进行扣款的订单）；

        protected string v_pstring;	//支付状态描述
        protected string v_pmode;	//支付银行
        protected string v_amount;	//支付金额
        protected string v_moneytype;	//币种		
        protected string remark1;	// 备注1
        protected string remark2;	// 备注1
        protected string v_md5str;
        protected string status_msg;
        protected string str;	   // 备注1

        protected NJL.BLL.NJL_SYS_CS_PAY_MAP bllPayMap = new BLL.NJL_SYS_CS_PAY_MAP();
        NJL.Model.NJL_SYS_CS_PAY_MAP modelPayMap;
        protected void Page_Load(object sender, EventArgs e)
        {
            modelPayMap = bllPayMap.GetModel("3");
            // MD5密钥要跟订单提交页相同，如Send.asp里的 key = "test" ,修改""号内 test 为您的密钥
            string key = modelPayMap.PAY_COMPANY_KEY;	// 如果您还没有设置MD5密钥请登陆我们为您提供商户后台，地址：https://merchant3.chinabank.com.cn/
            // 登陆后在上面的导航栏里可能找到“B2C”，在二级导航栏里有“MD5密钥设置”
            // 建议您设置一个16位以上的密钥或更高，密钥最多64位，但设置16位已经足够了

            v_oid = Request["v_oid"];
            v_pstatus = Request["v_pstatus"];
            v_pstring = Request["v_pstring"];
            v_pmode = Request["v_pmode"];
            v_md5str = Request["v_md5str"];
            v_amount = Request["v_amount"];
            v_moneytype = Request["v_moneytype"];
            remark1 = Request["remark1"];
            remark2 = Request["remark2"];

            string str = v_oid + v_pstatus + v_amount + v_moneytype + key;

            str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToUpper();

            if (str == v_md5str)
            {

                if (v_pstatus.Equals("20"))
                {
                    //支付成功
                    //在这里商户可以写上自己的业务逻辑
                    NJL.BLL.NJL_BOOK_ORDER bll = new NJL.BLL.NJL_BOOK_ORDER();
                    NJL.Model.NJL_BOOK_ORDER model = bll.GetModelByOrderNO(v_oid);
                    model.ORDER_STATE = 2;          //付款成功，完成订单
                    model.PAY_MAP_TYPE_CODE = "3";//更新支付方式
                    model.PAY_DATE_TIME = DateTime.Now;
                    bll.Update(model);
                    NJL.BLL.NJL_BOOK bllBook = new NJL.BLL.NJL_BOOK();
                    NJL.Model.NJL_BOOK modelBook = bllBook.GetModel(model.NJL_BOOK_ID);
                    NJL.BLL.NJL_REG_PAY bllRegPay = new NJL.BLL.NJL_REG_PAY();
                    NJL.Model.NJL_REG_PAY modelRegPay;
                    modelRegPay = new NJL.Model.NJL_REG_PAY();
                    modelRegPay.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                    modelRegPay.NJL_SYS_BASE_ID = WEBSITE_CODE;
                    modelRegPay.BOOK_ORDER_ID = model.ID;
                    modelRegPay.ORDER_NO = v_oid;
                    modelRegPay.ORDER_TOTAL = Convert.ToDecimal(v_amount);
                    modelRegPay.ORDER_TYPE_CODE = modelBook.TYPE_CODE;
                    modelRegPay.PAY_MAP_TYPE_CODE = "3";        //3为网银在线
                    modelRegPay.CREATE_DATE_TIME = DateTime.Now;
                    modelRegPay.CREATE_USER_ID = UserId;
                    modelRegPay.CREATE_USER_NAME = UserName;
                    if (bllRegPay.Add(modelRegPay))
                    {

                    }
                    if (modelBook.TYPE_CODE == "1")
                    {
                        Response.Redirect("/AttractionsOrderOK.aspx?id=" + model.ID, true);
                    }
                    else if (modelBook.TYPE_CODE == "2")
                    {
                        if (model.IS_REGISTER_USER == 1)
                        {
                            Response.Redirect("/CompanyOrderOK.aspx?id=" + model.ID, true);
                        }
                        else
                        {
                            Response.Redirect("/CompanyOrderOK2.aspx?id=" + model.ID, true);
                        }
                    }
                }

            }
            else
            {

                status_msg = "校验失败，数据可疑";
            }
        }
    }
}